package www.wf.pro.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import www.wf.pro.enums.ResultEnum;

import java.io.Serializable;

/**
 * @author wangfei
 * @date 2020-07-27 20:44
 */
@ApiModel(value = "公共模块")
public class ResultModel<T> implements Serializable {
    /**
     * 日志
     */
    private static final Logger log = LoggerFactory.getLogger(ResultModel.class);

    @ApiModelProperty(value = "标识码", name = "code")
    private Integer code;

    @ApiModelProperty(value = "返回数据")
    private T data;

    @ApiModelProperty(value = "成功状态")
    private boolean isSuccess;

    @ApiModelProperty("返回信息")
    private String message;

    public ResultModel() {
    }


    public ResultModel (ResultEnum resultEnum){
        if(resultEnum.getCode().equals(ResultEnum.SUCCESS.getCode())){
            isSuccess = true;
        }else{
            isSuccess = false;
        }
        code = resultEnum.getCode();
        message = resultEnum.getMessage();
    }

    public ResultModel(ResultEnum resultEnum, String detail) {
        if (resultEnum.getCode().equals(ResultEnum.SUCCESS.getCode())) {
            isSuccess = true;
        } else {
            isSuccess = false;
        }
        code = resultEnum.getCode();
        message = resultEnum.getMessage() + " : " + detail;
    }

    /**
     * 自定义消息
     * @param messages
     */
    public ResultModel(String messages) {
        code = ResultEnum.FAIL.getCode();
        message = messages;
    }

    /**
     * 成功返回参数
     * @param t
     */
    public ResultModel(T t) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.message = ResultEnum.SUCCESS.getMessage();
        this.data = t;
    }

    /**
     * 参数缺失
     *
     * @return
     */
    public static <T> ResultModel<T> checkParamFail() {
        return new ResultModel<>(ResultEnum.NO_PARAM);
    }

    /**
     * 参数缺失【自定义消息】
     *
     * @return
     */
    public static <T> ResultModel<T> checkParamFail(String detail) {
        return new ResultModel<>(ResultEnum.NO_PARAM, detail);
    }

    /**
     * 操作失败
     *
     * @return
     */
    public static ResultModel error() {
        return new ResultModel(ResultEnum.ERROR);
    }

    /**
     * 自定义异常消息
     */
    public static ResultModel fail(String message) {
        return new ResultModel(message);
    }

    /**
     * 操作失败 默认201
     *
     * @return
     */
    public static ResultModel fail() {
        return new ResultModel(ResultEnum.FAIL);
    }

    /**
     * Feign容错
     * @return
     */
    public static ResultModel faultTolerant() {
        return new ResultModel(ResultEnum.FAULT_TOLERANT);
    }
    /**
     * 无数据
     */
    public static ResultModel noData() {
        return new ResultModel(ResultEnum.NO_DATA);
    }

    /**
     * 操作成功带参数
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> ResultModel success(T t) {
        return new ResultModel(t);
    }

    /**
     * 操作成功不带参数
     *
     * @return
     */
    public static ResultModel success() {
        return new ResultModel(ResultEnum.SUCCESS);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public boolean isSuccess() {
        if (code == null) {
            return false;
        }
        if (code.equals(ResultEnum.SUCCESS.getCode())) {
            return true;
        }
        return false;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
